/**
 * 温控计历史记录
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");
const {timeComponent,chartDataProcessing} = require("../../libs/Utils")
const columnNameObj = require("./deviceChartConfig").chart.VAVTemperature;
const deviceType = require("./deviceChartConfig").type.VAVTemperature;

module.exports = async function (params) {
    try{
    let timeInterval = timeComponent(params.startTime,params.endTime,params.type);
    let sql = `SELECT
        dd.device_no AS deviceNo,
        DATE_FORMAT(dd.data_time,"%Y-%m-%d %H:%i:%s") AS dataTime,
        ${timeInterval.queryGroupTime} 
        df.device_name AS deviceName,
        dd.temperature_mode AS temperatureMode,
        dd.preset_temperature AS presetTemperature,
        dd.indoor_temperature AS indoorTemperature,
        dd.power_mode AS powerMode,
        dd.actual_airflow AS actualAirflow,   
        dd.required_airflow AS requiredAirflow,   
        dd.valve_position_feedback AS valvePositionFeedback,
        dd.max_airflow AS maxAirflow,
        dd.min_airflow AS minAirflow,
        dd.duct_diameter AS ductDiameter,
        dd.duct_k_factor AS ductKFactor,   
        dd.cooling_lower_limit AS coolingLowerLimit,   
        dd.heating_upper_limit AS coolingMediumSettingAccumulated,
        dd.fault_information AS faultInformation,
        rf.range_name AS rangeName
    FROM
    data_vav_temperature_${params.userDomainId} dd
    INNER JOIN device_file df ON df.device_no = dd.device_no
    AND df.delete_sign = 1 AND df.device_type = ${deviceType}
    AND df.range_id IN (?)
    INNER JOIN range_file rf ON df.range_id = rf.range_id AND rf.delete_sign = 1 
    WHERE 1 = 1 ${timeInterval.startTime && timeInterval.endTime ? ` AND (dd.data_time > "${timeInterval.startTime}" AND dd.data_time <= "${timeInterval.endTime}")` : `` } 
    ORDER BY dd.data_time DESC`;
    let vavTemperatureDataList = await conn(sql,[params.rangeIds]);
    let vavTemperatureDataGroup = _.groupBy(vavTemperatureDataList,"deviceNo");
    let dataList = [];
    for(const deviceNo in vavTemperatureDataGroup){
        let deviceDataList = vavTemperatureDataGroup[deviceNo];
        let deviceObj = {
            deviceName:deviceDataList[0].deviceName,
            rangeName:deviceDataList[0].rangeName,
            deviceNo:deviceNo,
            presetTemperature:deviceDataList[0].presetTemperature,
            indoorTemperature:deviceDataList[0].indoorTemperature,
            powerMode:deviceDataList[0].powerMode,
            actualAirflow:deviceDataList[0].actualAirflow,
            requiredAirflow:deviceDataList[0].requiredAirflow,
            valvePositionFeedback:deviceDataList[0].valvePositionFeedback,
            maxAirflow:deviceDataList[0].maxAirflow,
            minAirflow:deviceDataList[0].minAirflow,
            ductDiameter:deviceDataList[0].ductDiameter,
            ductKFactor:deviceDataList[0].ductKFactor,
            coolingLowerLimit:deviceDataList[0].coolingLowerLimit,
            coolingMediumSettingAccumulated:deviceDataList[0].coolingMediumSettingAccumulated,
            faultInformation:deviceDataList[0].faultInformation,
            dataList:[],
        }
        if(params.type === "hour"){
            deviceObj.dataList = deviceDataList
        }else {
            let timeGroup = _.groupBy(deviceDataList,"groupTime");
            for(const time in timeGroup){
                let timeGroupList = timeGroup[time];
                let deviceDataObj = {
                    dataTime:time,
                    dataCreateTime:timeGroupList[timeGroupList.length - 1].dataTime,
                    presetTemperature:timeGroupList[0].presetTemperature,
                    indoorTemperature:timeGroupList[0].indoorTemperature,
                    powerMode:timeGroupList[0].powerMode,
                    actualAirflow:timeGroupList[0].actualAirflow,
                    requiredAirflow:timeGroupList[0].requiredAirflow,
                    valvePositionFeedback:timeGroupList[0].valvePositionFeedback,
                    maxAirflow:timeGroupList[0].maxAirflow,
                    minAirflow:timeGroupList[0].minAirflow,
                    ductDiameter:timeGroupList[0].ductDiameter,
                    ductKFactor:timeGroupList[0].ductKFactor,
                    coolingLowerLimit:timeGroupList[0].coolingLowerLimit,
                    coolingMediumSettingAccumulated:timeGroupList[0].coolingMediumSettingAccumulated,
                    faultInformation:timeGroupList[0].faultInformation,
                }
                deviceObj.dataList.push(deviceDataObj);
            }
        }
        deviceObj.chart = chartDataProcessing(deviceObj.dataList,columnNameObj);
        dataList.push(deviceObj);
    }
    return { success: true,dataList:dataList};
    }catch (e){
        return { success: false,dataList:[],message:e.code === "ER_NO_SUCH_TABLE" ? "当前账号无此类设备" : e.sqlMessage};
    }
};